import urllib.request
import os
import sys
from PIL import Image

# img,图片地址
i = sys.argv[1]
# path,保存路径
p = sys.argv[2]

img_request = urllib.request.Request(i)
img_request.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
img_response = urllib.request.urlopen(img_request)
img = img_response.read()

with open(p, 'wb') as f:
    f.write(img)

try:
    _img = Image.open(p)    # 如果图片不存在，报错FileNotFoundError
    _img.load()    # 如果图片不完整，报错OSError: image file is truncated
except (FileNotFoundError, OSError) as exception :
    print(exception)
    os.remove(p)